/**
 * 一个解析文件路径的扩展，默认通过斜杠(/)做分隔，
 * 支持windows和linux的路径格式解析
 * linux:/home/tiger/doc/hi.txt
 * windows:C:\Users\Administrator\hi.txt
 */
(function ($) {
  $.extend({
    /**
     * 解析完整的文件路径名
     * @param {string} fileName 文件路径
     * @param {string} separator 分隔符
     */
    fileNameAnalysis: function (fileName, separator) {
      if (!fileName) {
        throw new Error("请传入文件路径参数");
      }
      if (fileName.length === 0) {
        return {
          nameArr: [],
        };
      }
      if (!separator) {
        separator = "/";
      }
      var nameArr = fileName.split(separator);
      var nameArr2 = [];
      for (var i = 0; i < nameArr.length; i++) {
        if (nameArr[i].trim() === "") {
          continue;
        }
        nameArr2.push(nameArr[i]);
      }
      nameArr = nameArr2;
      //hi.txt
      var fullFileName = nameArr[nameArr.length - 1];
      var lastDotIndex = fullFileName.lastIndexOf(".");
      //hi
      var fileNameWithoutExtName = fullFileName.substring(0, lastDotIndex);
      //.txt
      var extName = fullFileName.substring(lastDotIndex, fullFileName.length);
      //txt
      var extNameWithoutPrefix = extName.substring(1);
      var obj = {
        sourceFileName: fileName,
        nameArr: nameArr,
        fileName: fullFileName,
        fileNameWithoutExtName: fileNameWithoutExtName,
        extName: extName,
        extNameWithoutPrefix: extNameWithoutPrefix,
      };
      return obj;
    },
  });
})(jQuery);
